use std::sync::Arc;

use axum::{async_trait, extract::FromRequestParts, http::header, http::request::Parts};

use crate::{AppState, Error};

pub struct ClientInfo {
    pub ip: String,
    pub user_agent: String,
}

#[async_trait]
impl FromRequestParts<Arc<AppState>> for ClientInfo {
    type Rejection = Error;

    async fn from_request_parts(
        parts: &mut Parts,
        _state: &Arc<AppState>,
    ) -> std::result::Result<Self, Self::Rejection> {
        let user_agent = parts
            .headers
            .get(header::USER_AGENT)
            .and_then(|value| value.to_str().ok());
        let user_agent = user_agent.unwrap_or_default();

        Ok(ClientInfo {
            ip: "".to_string(),
            user_agent: user_agent.to_string(),
        })
    }
}
